﻿using Microsoft.Ink;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;


namespace InputUserControl
{

    public partial class UCHandWrite : UserControl
    {
        public UCHandWrite()
        {
            InitializeComponent();
            m_inkCollector = new InkCollector(this.picInk.Handle);
            m_inkCollector.Stroke += new InkCollectorStrokeEventHandler(m_inkCollector_Stroke);
            m_inkCollector.AutoRedraw = true;
            m_inkCollector.DefaultDrawingAttributes.AntiAliased = true;
            m_inkCollector.Enabled = true;
            Recognizers recos = new Recognizers();
            Recognizer chineseReco = recos.GetDefaultRecognizer();
            m_recognizerContext = chineseReco.CreateRecognizerContext();
            m_recognizerContext.RecognitionWithAlternates += new RecognizerContextRecognitionWithAlternatesEventHandler(m_recognizerContext_RecognitionWithAlternates);
            m_recognizerContext.Strokes = m_inkCollector.Ink.Strokes;
            m_btnRecs = new Button[ARRAY_SIZE] { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8 };
            InitImages();
        }


        private string m_strLogName = "UCHandWrite";

        private const int ARRAY_SIZE = 8;
        private InkCollector m_inkCollector=null;
        private RecognizerContext m_recognizerContext = new RecognizerContext();
        private Button[] m_btnRecs = null;
        private string[] m_strRecs = new string[ARRAY_SIZE];
        private string m_strCurDir = AppDomain.CurrentDomain.BaseDirectory;


        public delegate void DRecognizer(string strRecText);
        public event DRecognizer ERecognizer;




        void InitImages()
        {
            string strClass = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string strLog = "In";
            XDLog.WriteLog(m_strLogName, strClass, strLog);
            try
            {
                string strImageDir = m_strCurDir + "Images";
                string strClearHandWriteImg = strImageDir + "\\ClearHandWrite.png";
                string strHandWriteAreaImg = strImageDir + "\\HandWriteArea.jpg";
                string strWuBiImg = strImageDir + "\\WuBi.png";
                string strPPZImg = strImageDir + "\\PPZ.png";
                string strFuHaoImg = strImageDir + "\\FuHao.png";
                if (File.Exists(strPPZImg))
                {
                    for (int nIndex = 0; nIndex < m_btnRecs.Length; nIndex++)
                    {
                        this.m_btnRecs[nIndex].BackgroundImage = Image.FromFile(strPPZImg);
                        this.m_btnRecs[nIndex].BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
                    }
 
                }
                if (File.Exists(strClearHandWriteImg))
                {
                    this.btnClear.BackgroundImage = Image.FromFile(strClearHandWriteImg);
                    this.btnClear.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
                }
                if (File.Exists(strFuHaoImg))
                {
                    this.btnHeng.BackgroundImage = Image.FromFile(strFuHaoImg);
                    this.btnHeng.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;

                    this.btnJinHao.BackgroundImage = Image.FromFile(strFuHaoImg);
                    this.btnJinHao.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
                }
                if (File.Exists(strHandWriteAreaImg))
                {
                    this.picInk.BackgroundImage = Image.FromFile(strHandWriteAreaImg);
                    this.picInk.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
                }
            }
            catch (Exception ex)
            {
                strLog = ex.Message;
                XDLog.WriteLog(m_strLogName, strClass, strLog);
                ShowMessageBoxError(strLog, strClass);
            }
            strLog = "Out";
            XDLog.WriteLog(m_strLogName, strClass, strLog);
        }


        void m_inkCollector_Stroke(object sender, InkCollectorStrokeEventArgs e)
        {
            string strClass = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string strLog = "In";
            XDLog.WriteLog(m_strLogName, strClass, strLog);
            try
            {
                m_recognizerContext.StopBackgroundRecognition();
                m_recognizerContext.Strokes.Add(e.Stroke);
                //m_recognizerContext.CharacterAutoCompletion = RecognizerCharacterAutoCompletionMode.Full;
                m_recognizerContext.BackgroundRecognizeWithAlternates(0);
            }
            catch (Exception ex)
            {
                strLog = ex.Message;
                XDLog.WriteLog(m_strLogName, strClass, strLog);
                ShowMessageBoxError(strLog, strClass);
            }
            strLog = "Out";
            XDLog.WriteLog(m_strLogName, strClass, strLog);
        }


        void m_recognizerContext_RecognitionWithAlternates(object sender, RecognizerContextRecognitionWithAlternatesEventArgs e)
        {
            string strClass = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string strLog = "In";
            XDLog.WriteLog(m_strLogName, strClass, strLog);
            try
            {
                RecognitionAlternates alts;

                if (e.RecognitionStatus == RecognitionStatus.NoError)
                {
                    alts = e.Result.GetAlternatesFromSelection();

                    int nIndex = 0;
                    string strText = "";
                    foreach (RecognitionAlternate alt in alts)
                    {
                        strText += alt.ToString() + " ";
                        if (nIndex > 7)
                        {
                            break;
                        }
                        m_strRecs[nIndex] = alt.ToString();
                        nIndex++;
                    }

                    int kkk = 1;
                }
                Control.CheckForIllegalCrossThreadCalls = false;
                for (int nIndex = 0; nIndex < m_btnRecs.Length; nIndex++)
                {
                    m_btnRecs[nIndex].Text = m_strRecs[nIndex];
                }
                Control.CheckForIllegalCrossThreadCalls = true;
            }
            catch (Exception ex)
            {
                strLog = ex.Message;
                XDLog.WriteLog(m_strLogName, strClass, strLog);
                ShowMessageBoxError(strLog, strClass);
            }
            strLog = "Out";
            XDLog.WriteLog(m_strLogName, strClass, strLog);
        }



        #region 窗体布局属性



        //public override void Init()
        //{
            //string strClass = System.Reflection.MethodBase.GetCurrentMethod().Name;
            //string strLog = "In";
            //XDLog.WriteLog(m_strLogName, strClass, strLog);
            //try
            //{
            //    if (!m_parseUCXml.LoadXml(m_strXmlFile, ref strLog))
            //    {
            //        Event_Init(strLog);
            //        return;
            //    }
            //    if (!CreateControls(ref strLog))
            //    {
            //        Event_Init(strLog);
            //        return;
            //    }

            //}
            //catch (Exception ex)
            //{
            //    strLog = ex.Message;
            //    XDLog.WriteLog(m_strLogName, strClass, strLog);
            //    Event_Init(strLog);
            //}
            //strLog = "Out";
            //XDLog.WriteLog(m_strLogName, strClass, strLog);
        //}



        //private bool CreateControls(ref string ErrMsg)
        //{
        //    string strClass = System.Reflection.MethodBase.GetCurrentMethod().Name;
        //    string strLog = "In";
        //    bool bOK = true;
        //    XDLog.WriteLog(m_strLogName, strClass, strLog);
        //    try
        //    {
        //        m_lstBtns.Clear();
        //        SuspendLayout();
        //        if (!CreateButtons(ref ErrMsg))
        //        {
        //            return false;
        //        }
        //        ResumeLayout(false);
               
        //    }
        //    catch (Exception ex)
        //    {
        //        return bOK;
        //        strLog = ex.Message;
        //        XDLog.WriteLog(m_strLogName, strClass, strLog);
        //    }
        //    strLog = "Out";
        //    XDLog.WriteLog(m_strLogName, strClass, strLog);
        //    return bOK;
        //}




        #endregion


        #region 按钮事件



        /// <summary>
        /// 
        /// </summary>
        /// <param name="ErrMsg"></param>
        /// <returns></returns>
        //private bool CreateButtons(ref string ErrMsg)
        //{
            //string strClass = System.Reflection.MethodBase.GetCurrentMethod().Name;
            //string strLog = "In";
            //bool bOK=true;
            //XDLog.WriteLog(m_strLogName, strClass, strLog);
            //try
            //{
            //    UCControlStruct m_UCStruct = m_parseUCXml.UCControl;

            //    for (int nIndex = 0; nIndex < m_UCStruct.lstButtons.Count; nIndex++)
            //    {
            //        ButtonStruct btnStruct = m_UCStruct.lstButtons[nIndex];
            //        Button button = new Button();
            //        button.Location = new System.Drawing.Point(btnStruct.X, btnStruct.Y);
            //        button.Size = new System.Drawing.Size(btnStruct.Width, btnStruct.Height);
            //        button.Visible = btnStruct.IsShow;
            //        button.Text = btnStruct.Text;
            //        button.Font = new Font(btnStruct.Font.FontName, btnStruct.Font.FontSize);
            //        button.ForeColor = Color.FromArgb(btnStruct.Font.R,btnStruct.Font.G,btnStruct.Font.B);
            //        if (File.Exists(btnStruct.BackgroundImage))
            //        {
            //            try
            //            {
            //                button.Image = Image.FromFile(btnStruct.BackgroundImage);
            //            }
            //            catch (Exception ex)
            //            {
            //                strLog = ex.Message;
            //                XDLog.WriteLog(m_strLogName, strClass, strLog);
            //            }
            //        }
            //        button.Name = btnStruct.Name;
            //        button.Click += new EventHandler(Button_Click);
            //        button.Enabled = btnStruct.IsEnable;
            //        m_lstBtns.Add(button);
            //        this.Controls.Add(button);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    bOK=false;
            //    ErrMsg = ex.Message;
            //    XDLog.WriteLog(m_strLogName, strClass, ErrMsg);
            //}
            //strLog = "Out";
            //XDLog.WriteLog(m_strLogName, strClass, strLog);
            //return bOK;
        //}



        //public void ShowButtons(bool bClickOK)
        //{
            //string strClass = System.Reflection.MethodBase.GetCurrentMethod().Name;
            //string strLog = "In";
            //XDLog.WriteLog(m_strLogName, strClass, strLog);
            //try
            //{
                //if (!bClickOK)
                //{
                //    m_strNowInputName = m_strPreInputName;
                //}

                //for (int nIndex = 0; nIndex < m_lstBtnAttributes.Count; nIndex++)
                //{
                //    if (m_lstBtnAttributes[nIndex].Name == m_strNowInputName)
                //    {
                //        m_lstBtns[nIndex].Enabled = false;
                //    }
                //    else
                //    {
                //        m_lstBtns[nIndex].Enabled = true;
                //    }
                //}

            //}
            //catch (Exception ex)
            //{
            //    strLog = ex.Message;
            //    XDLog.WriteLog(m_strLogName, strClass, strLog);
            //    MessageBox.Show(strLog, strClass, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            //strLog = "Out";
            //XDLog.WriteLog(m_strLogName, strClass, strLog);
        //}



        //private void Button_Click(object sender, EventArgs e)
        //{
            //string strClass=System.Reflection.MethodBase.GetCurrentMethod().Name;
            //string strLog="In";
            //XDLog.WriteLog(m_strLogName, strClass, strLog);
            //try
            //{

                //if (EButtonClick_InputType != null)
                //{
                //    Button btn = sender as Button;
                //    if (btn != null)
                //    {
                //        m_strPreInputName = m_strNowInputName;
                //        m_strNowInputName = btn.Name;
                //        strLog = string.Format("ButtonName[{0}]", m_strNowInputName);
                //        XDLog.WriteLog(m_strLogName, strClass, strLog);
                //        EButtonClick_InputType(this.Name, m_strNowInputName);
                //    }
                //}
                //else
                //{
                //    strLog = "EButtonClick is null";
                //    XDLog.WriteLog(m_strLogName, strClass, strLog);
                //}
            //}
            //catch (Exception ex)
            //{
            //    strLog = ex.Message;
            //    XDLog.WriteLog(m_strLogName, strClass, strLog);
            //    MessageBox.Show(strLog, strClass, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            //strLog = "Out";
            //XDLog.WriteLog(m_strLogName, strClass, strLog);
        //}



        #endregion


        #region 消息框

        private void ShowMessageBoxError(string MsgText, string Title)
        {
            MessageBox.Show(MsgText, Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void ShowMessageBoxWarning(string MsgText, string Title)
        {
            MessageBox.Show(MsgText, Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void ShowMessageBoxOK(string MsgText, string Title)
        {
            MessageBox.Show(MsgText, Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        #endregion



        private void Button_Recognizer_Click(object sender, EventArgs e)
        {
            string strClass = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string strLog = "In";
            XDLog.WriteLog(m_strLogName, strClass, strLog);
            try
            {
                Button btn = sender as Button;
                if (btn!=null && ERecognizer != null && !string.IsNullOrEmpty(btn.Text))
                {
                    ERecognizer(btn.Text);
                }
                ClearData();
            }
            catch (Exception ex)
            {
                strLog = ex.Message;
                XDLog.WriteLog(m_strLogName, strClass, strLog);
                ShowMessageBoxError(strLog, strClass);
            }
            strLog = "Out";
            XDLog.WriteLog(m_strLogName, strClass, strLog);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            string strClass = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string strLog = "In";
            XDLog.WriteLog(m_strLogName, strClass, strLog);
            try
            {
                ClearData();
            }
            catch (Exception ex)
            {
                strLog = ex.Message;
                XDLog.WriteLog(m_strLogName, strClass, strLog);
                ShowMessageBoxError(strLog, strClass);
            }
            strLog = "Out";
            XDLog.WriteLog(m_strLogName, strClass, strLog);
        }


        private void ClearData()
        {
            //for (int nIndex = 0; nIndex < m_btnRecs.Length; nIndex++)
            //{
            //    m_btnRecs[nIndex].Text = "";
            //    m_strRecs[nIndex] = "";
            //}
            if (!m_inkCollector.CollectingInk)
            {
                Strokes strokesToDelete = this.m_inkCollector.Ink.Strokes;
                m_recognizerContext.StopBackgroundRecognition();
                m_inkCollector.Ink.DeleteStrokes(strokesToDelete);
                m_recognizerContext.Strokes = m_inkCollector.Ink.Strokes;
                m_inkCollector.Ink.DeleteStrokes();//清除手写区域笔画;
                this.picInk.Refresh();//刷新手写区域
            }
        }



    }
}